'use strict';

module.exports = app => {
  return class extends app.Controller {
    * index(ctx) {
        const query=ctx.request.query;
        var pageParams='';
        if(query.pageParams){
            pageParams =JSON.parse(ctx.request.query.pageParams);
            if(!pageParams.order){
                pageParams.order=[['createdAt','DESC']];
            }
        }else {
            pageParams={
                pageNum:1,
                pageLimit:24,
                pageTotal:0,
                order:[['createdAt','DESC']],
                where:null
            }
        }
        let where=pageParams.where ? pageParams.where :null;
        var offset=(pageParams.pageNum - 1) * pageParams.pageLimit;
        var articles=yield ctx.model.Tag.findAndCount({where:pageParams.where,limit:pageParams.pageLimit,offset:offset,order:pageParams.order});
        pageParams.pageTotal=Math.ceil(articles.count/pageParams.pageLimit);
        console.log('*******************');
        console.log(new Date());
        yield ctx.body={
            data:articles.rows,
            pageParams:pageParams
        };

    }
    * show(ctx){
        const id=ctx.params.id;
        var article=yield ctx.model.Article.findById(id);
        article.view++;
        article.save();
        ctx.body=article;
    }
      * create(ctx){
          let body=ctx.request.body;
          let data=yield ctx.model.Nice.create({articleId:body.articleId,userId:ctx.user.id});
          let article=yield ctx.model.Article.update({nice:ctx.model.literal('nice+1')},{where:{id:body.articleId}});
          if(data){
              ctx.body={
                  message:"新增成功！"
              }
          }
      }
  };
};
